package com.lisn.opengl.util;


import android.os.Build;
import android.util.Size;

import androidx.annotation.RequiresApi;
import androidx.camera.core.CameraX;
import androidx.camera.core.Preview;
import androidx.camera.core.PreviewConfig;
import androidx.lifecycle.LifecycleOwner;

/**
 * @author : lishan
 * @e-mail : cnlishan@163.com
 * @date : 2020/8/24 2:39 PM
 * @desc :
 */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class CameraXHelper {

    private static CameraX.LensFacing currentFacing = CameraX.LensFacing.BACK;


    public CameraXHelper(LifecycleOwner lifecycleOwner
            , Preview.OnPreviewOutputUpdateListener listener) {
        CameraX.bindToLifecycle(lifecycleOwner, getPreView(listener));

    }


    private static Preview getPreView(Preview.OnPreviewOutputUpdateListener listener) {
        //1. preview
        PreviewConfig config = new PreviewConfig.Builder()
                //.setTargetRotation(mTextureView.getDisplay().getRotation())
                .setTargetResolution(new Size(640, 480))
                .setLensFacing(currentFacing)
                .build();

        Preview preview = new Preview(config);
        preview.setOnPreviewOutputUpdateListener(listener);
        return preview;
    }
}
